"""路径转换与处理"""

import contextlib
import os
from pathlib import Path
from typing import Union


def path_from_root(root: Path, path_str: Union[str, Path]) -> Path:
    """
    If path is relative, prepend root
    If path is absolute, return it directly.
    """
    root = Path(os.path.expanduser(str(root)))
    path = Path(path_str)
    if not path.is_absolute():
        path = root / path
    return path.resolve()


def mkdir(path_str: Union[str, Path]) -> None:
    """
    Create the existing directory (and its parents) if necessary.
    """
    path = Path(path_str)
    path.mkdir(parents=True, exist_ok=True)


def make_path_relative(path_str: Union[str, Path], root: Path) -> Path:
    """
    Try to make the given path relative, given the default root.
    """
    path = Path(path_str)
    with contextlib.suppress(ValueError):
        path = path.relative_to(root)
    return path


def get_path_and_file_name(path_str: Union[str, Path]) -> tuple:
    """根据路径获取文件夹名称跟文件名称"""
    return os.path.split(path_str)


if __name__ == '__main__':
    print(get_path_and_file_name("/home/jet/workspace/python/crawler-master/run.py"))  # noqa:E501
